export default class RunContext{

    static getInstance( ) {
        if(!this.instance) {
            this.instance =new RunContext();
        }
        return this.instance;
    }

    constructor(){
        this.runEntitys=[];
        this.suspend=0;;
    }

    next(){
        let _this=this;
        if(_this.runEntitys.length >0 && _this.suspend ==0){

            let res=_this.runEntitys[_this.runEntitys.length-1].run(_this.next);
            res.then(res=>{
                let runEntity= _this.runEntitys[_this.runEntitys.length-1];
                if(runEntity.coordinate >= runEntity.commands.length ){
                    _this.runEntitys.splice(_this.runEntitys.length-1,1);
                }
                _this.next()
            })
        }

    }

    add(runEntitys){
        let _this=this;
        this.runEntitys.push(runEntitys)
        setTimeout(()=>{this.next()})
        return this;
    }
}